/*
*@module: GroundInfo_Controller
*@author: 陈刚
*/

const AllGroundDataModel = require('../../models/table/groundInfo.js')
const AllMap = require('../../models/common/Map.js')

var reference_17_ground_Map, element_Map, unit_Map

AllMap.then(data=>{
  reference_17_ground_Map = data.reference_17_ground_Map
  element_Map = data.element_Map
  unit_Map = data.unit_Map_ground
})


class GroundInfo_Controller {

  /**
   *获取选择的项目体、监测点位、调查类型的对应某一页土壤-实验室样本列表
   * @param ctx project_num 项目体编号
   * @param ctx point_num 监测点位编号
   * @param ctx assess_type 调查类型
   * @param ctx currentPage 页码
   * @param ctx page_size 一页显示数量
   *
   * @memberof GroundInfo_Controller
   */
  static async getLabGroundList(ctx) {
    let req = ctx.request.body
    let { selectedProject: project_num, selectedPoint: point_num, selectedAssessType: assess_type, currentPage, page_size } = req
    let labGroundData = await AllGroundDataModel.getLabGroundData(project_num, point_num, assess_type, currentPage, page_size)
    let rows = labGroundData.rows
    let count = labGroundData.count

    let SelectLabGroundData = rows.map(item => {
      let labGround_obj = item.dataValues
      let temp_obj = {}
      let LabGroundData_arr = []
      for(let key in labGround_obj){
        if(reference_17_ground_Map.has(key)){
          if(labGround_obj[key] < reference_17_ground_Map.get(key)){
            LabGroundData_arr.push({
              element: key,
              value: labGround_obj[key],
              reference: reference_17_ground_Map.get(key),
              ispollution: 0,
              unit: unit_Map.get(key),
              time: labGround_obj.date
            })
          }else{
            LabGroundData_arr.push({
              element: key,
              value: labGround_obj[key],
              reference: reference_17_ground_Map.get(key),
              ispollution: 1,
              unit: unit_Map.get(key),
              time: labGround_obj.date
            })
          }
          temp_obj.labGround_element = LabGroundData_arr
        }else{
          temp_obj[key] = labGround_obj[key]
        }
      }
      return item =temp_obj

    })


    if (SelectLabGroundData) {

      ctx.body = {
        success: true,
        res: SelectLabGroundData,
        count: count,
        element_Map: Array.from(element_Map), //似乎Map数据不能直接传递，要先转换为数组，前端再转换为Map
        msg: '获取成功'
      }
    } else {
      ctx.body = {
        success: true,
        res: [],
        count: 0,
        msg: '没有监测点位信息'
      }
    }
  }


  /**
   *获取选择的项目体、监测点位、调查类型的对应某一页土壤-检测样本列表
   * @param ctx project_num 项目体编号
   * @param ctx point_num 监测点位编号
   * @param ctx assess_type 调查类型
   * @param ctx currentPage 页码
   * @param ctx page_size 一页显示数量
   *
   * @memberof GroundInfo_Controller
   */
  static async getDetGroundList(ctx) {
    let req = ctx.request.body
    let { selectedProject: project_num, selectedPoint: point_num, selectedAssessType: assess_type, currentPage, page_size } = req
    let detGroundData = await AllGroundDataModel.getDetGroundData(project_num, point_num, assess_type, currentPage, page_size)
    let rows = detGroundData.rows
    let count = detGroundData.count

    let SelectDetGroundData = rows.map(item => {
      let detGround_obj = item.dataValues
      let temp_obj = {}
      let DetGroundData_arr = []
      for(let key in detGround_obj){
        if(reference_17_ground_Map.has(key)){
          if(detGround_obj[key] < reference_17_ground_Map.get(key)){
            DetGroundData_arr.push({
              element: key,
              value: detGround_obj[key],
              reference: reference_17_ground_Map.get(key),
              ispollution: 0,
              unit: unit_Map.get(key),
              time: detGround_obj.date
            })
          }else{
            DetGroundData_arr.push({
              element: key,
              value: detGround_obj[key],
              reference: reference_17_ground_Map.get(key),
              ispollution: 1,
              unit: unit_Map.get(key),
              time: detGround_obj.date
            })
          }
          temp_obj.detGround_element = DetGroundData_arr
        }else{
          temp_obj[key] = detGround_obj[key]
        }
      }
      return item =temp_obj

    })


    if (SelectDetGroundData) {
      ctx.body = {
        success: true,
        res: SelectDetGroundData,
        count: count,
        element_Map: Array.from(element_Map), //似乎Map数据不能直接传递，要先转换为数组，前端再转换为Map
        msg: '获取成功'
      }
    } else {
      ctx.body = {
        success: true,
        res: [],
        count: 0,
        msg: '没有监测点位信息'
      }
    }
  }

  /**
   *获取某样本的土壤实验室数据与检测仪数据的各元素对比情况
   * @param ctx sample_num 样本编号
   *
   * @memberof GroundInfo_Controller
   */
  static async getLabGroundReference(ctx){
    let sample_num = ctx.params.sample_num
    let labGroundRefData = await AllGroundDataModel.getLabGroundRefData(sample_num)

    let ref_arr = []

    let det_obj = labGroundRefData[1].dataValues
    if(labGroundRefData[0]){
      let lab_obj = labGroundRefData[0].dataValues
      for(let key in det_obj){
        if(reference_17_ground_Map.has(key)){
          ref_arr.push({
            element: key,
            value: det_obj[key],
            lab_reference: lab_obj[key],
            difference: Math.pow(lab_obj[key] - det_obj[key] , 2).toFixed(3),
            unit: unit_Map.get(key)
          })
        }
      }
    }else{
      for(let key in det_obj){
        if(reference_17_ground_Map.has(key)){
          ref_arr.push({
            element: key,
            value: det_obj[key],
            lab_reference: '-1',
            difference: '-/',
            unit: unit_Map.get(key)
          })
        }
      }
    }

    if (labGroundRefData) {
      ctx.body = {
        success: true,
        res: ref_arr,
        msg: '获取成功'
      }
    } else {
      ctx.body = {
        success: true,
        res: [],
        msg: '没有参考信息'
      }
    }
  }
}


module.exports = GroundInfo_Controller
